#ifndef ______WARD_GAMEOPTIONS_H
#define ______WARD_GAMEOPTIONS_H

#include "driver.h"
#include "sliderbar.h"
#include "checkbox.h"
#include "textbox.h"
#include "glstar.h"
#include "gltext.h"
#include "boxGroup.h"
#include "listBox.h"
#include "universebuilder.h"
#include "universe.h"

GAMESTATE displayGameOptionPage(GAMETYPE &reply)
{
	glClearColor(0.0,0.0,0.0,0.0);
	
	glDisable(GL_DEPTH_TEST);	
	glDisable(GL_LIGHTING);
	glDisable(GL_BLEND);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluOrtho2D(0,X_DIM,Y_DIM,0);

	textBox* t = new textBox(125,510,120," OK");
	textBox* q = new textBox(300,510,120," Cancel");
	boxGroup gameMode(100,340,CHECKBOX_MINIBOX,16,2,1,1,2);
	checkBox isScenario(100,240,16,CHECKBOX_MINIBOX);
	checkBox canPreview(750,550,16,CHECKBOX_MINIBOX);
	isScenario.setData(1.0);
	canPreview.setData(1.0);
	listBox scenarios(425,280,4,175,"campaigns.txt", true);

	sliderBar numPlanets(500,260,4,25,1);
	sliderBar connectedness(500,280,1,10,1);
	sliderBar numPlayers(500,240,2,(int)(numPlanets.getData()/2),1);
	float r = 0.2,g = 0.2,b = 0.2;
	int mult = 5;

	Star_C belt;
	belt.set2Dstars(2000);

	gameOptions data;
	data.noC = connectedness.getData();
	data.noPlay = numPlayers.getData();
	data.noPl = numPlanets.getData();
	data.randomGame = !isScenario.getData();
	data.file = scenarios.getText(scenarios.getData());
	BuildUniverse(data);

	Timer fps;
	SDL_Event event;
	universe_C* preview = new universe_C();
	preview->initialize(0);


	while(true)
	{
		fps.start();

		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);


		for(int i = 0; i < belt.NOS(); ++i)
		{
			if(i % 2 == 0)		{glColor4f(r, g, b, 0.9);}
			else				{glColor4f(1-r, 1-g, 1-b, 0.9);}

			glTranslatef(belt.sx(i),belt.sy(i),belt.sz(i));
			glutSolidSphere(belt.ss(i),10,10);
			glTranslatef(-belt.sx(i),-belt.sy(i),-belt.sz(i));
		}

		glLineWidth(8);
		glColor4f(0.5-r,0.5-g,0.5-b,1.0);
		writeText(0,200,2.0,2.0,"Game Options");
		glLineWidth(3.2);
		glColor4f(1-r,1-g,1-b,1.0);
		writeText(102,147,0.5,0.5,"Game Options");

		if(canPreview.getData())
			preview->displayConnect(r*0.5,g*0.5,b*0.5);
		glLineWidth(3.2);
		glColor4f(r,g,b,1.0);
		writeText(100,150,0.5,0.5,"Game Options");
		glLineWidth(1.0);
		glColor4f(1,1,1,1.0);
		writeText(125,170,0.1,0.1,"by the Mogs of War");
		writeText(0,600,0.1,0.1,"2007 Texas Tech University Computer Science");
		writeText(700,566,0.1,0.1,"Preview:");

		r += (0.001 * mult);
		g += (0.001 * mult);
		b += (0.001 * mult);

		if((r > 1)||(g > 1)||(b > 1)) mult = mult * -1;
		if((r < 0.2)||(g < 0.2)||(b < 0.2)) mult = mult * -1;

		t->draw();
		q->draw();
		isScenario.draw();
		gameMode.draw();		
		canPreview.draw();
		if(isScenario.getData())
		{
			scenarios.draw();
			glColor4f(1.0, 1.0, 1.0, 1.0);
			writeText(425,260,0.1,0.1,"Game Scenario:");
		}
		else
		{
			numPlayers.draw();
			connectedness.draw();
			numPlanets.draw();
			glColor4f(1.0, 1.0, 1.0, 1.0);
			writeText(370,255,0.1,0.1,"Number of players:");
			writeText(370,275,0.1,0.1,"Number of planets:");
			writeText(400,295,0.1,0.1,"Connectedness:");
			writeText(580,255,0.1,0.1,gitoa(numPlayers.getData(),10));
			writeText(580,275,0.1,0.1,gitoa(numPlanets.getData(),10));
			writeText(580,295,0.1,0.1,gitoa(connectedness.getData(),3));
		}

		writeText(100,325,0.1,0.1,"Game Mode:");
		writeText(120,355,0.1,0.1,"Conquest (51%)");
		writeText(120,375,0.1,0.1,"Domination (100%)");
		writeText(100,230,0.1,0.1,"Scenario Game?");

		string temp;

		glLineWidth(1.0);
		glColor4f(1,1,1,1.0);


		SDL_GL_SwapBuffers();

		if(t->getData() == 1)
		{
			data.file = scenarios.getText(scenarios.getData());
			data.noC = connectedness.getData();
			data.noPlay = numPlayers.getData();
			data.noPl = numPlanets.getData();
			data.randomGame = !isScenario.getData();
			for(int i = 0; i < 2; i++)
			{
				if(gameMode.getVal(i) == 1)
				{
					reply = (GAMETYPE)i;
					BuildUniverse(data);
					return STORY;
				}
			}
			t->onClick();
		}
		else if(q->getData() == 1)		{return TITLE;}

		while(SDL_PollEvent(&event))
		{
			if(event.type == SDL_KEYDOWN)
			{
				if(event.key.keysym.sym == SDLK_RETURN)
				{
					data.file = scenarios.getText(scenarios.getData());
					data.noC = connectedness.getData();
					data.noPlay = numPlayers.getData();
					data.noPl = numPlanets.getData();
					data.randomGame = !isScenario.getData();
					for(int i = 0; i < 2; i++)
					{
						if(gameMode.getVal(i) == 1)
						{
							reply = (GAMETYPE)i;
							BuildUniverse(data);
							return STORY;
						}
					}
				}
				else if(event.key.keysym.sym == SDLK_q)
				{return TITLE;}
			}
			if(event.type == SDL_MOUSEBUTTONDOWN)
			{
				if(t->clicked(event.button.x, event.button.y))
				{t->onClick();}
				if(q->clicked(event.button.x, event.button.y))
				{q->onClick();}

				if(canPreview.clicked(event.button.x, event.button.y))
				{
					canPreview.onClick();
				}

				if(gameMode.clicked(event.button.x, event.button.y))
				{

					gameMode.onClick();
					data.file = scenarios.getText(scenarios.getData());
					data.noC = connectedness.getData();
					data.noPlay = numPlayers.getData();
					data.noPl = numPlanets.getData();
					data.randomGame = !isScenario.getData();
					for(int i = 0; i < 2; i++)
					{
						if(gameMode.getVal(i) == 1)
						{
							reply = (GAMETYPE)i;
							BuildUniverse(data);
							delete preview;
							preview = new universe_C();
						}
					}
				
				}

				if(isScenario.getData())
				{
					if(scenarios.clicked(event.button.x, event.button.y))
					{
						scenarios.onClick();
						data.file = scenarios.getText(scenarios.getData());
						numPlanets.onClick();
						numPlayers.setRight(min(10,(int)(numPlanets.getData()/2)));
						numPlayers.setData(min(numPlayers.getData(),numPlanets.getData()/2));
						data.noC = connectedness.getData();
						data.noPlay = numPlayers.getData();
						data.noPl = numPlanets.getData();
						data.randomGame = !isScenario.getData();
						for(int i = 0; i < 2; i++)
						{
							if(gameMode.getVal(i) == 1)
							{
								reply = (GAMETYPE)i;
								BuildUniverse(data);
								delete preview;
								preview = new universe_C();
								preview->initialize(0);

							}
						}					
					}
					

				}
				else
				{
					if(connectedness.clicked(event.button.x, event.button.y))
					{
						connectedness.onClick();
						data.file = scenarios.getText(scenarios.getData());
						data.noC = connectedness.getData();
						data.noPlay = numPlayers.getData();
						data.noPl = numPlanets.getData();
						data.randomGame = !isScenario.getData();
						for(int i = 0; i < 2; i++)
						{
							if(gameMode.getVal(i) == 1)
							{
								reply = (GAMETYPE)i;
								BuildUniverse(data);
								delete preview;
								preview = new universe_C();
								preview->initialize(0);							}
						}
					
					}
					if(numPlanets.clicked(event.button.x, event.button.y))
					{
						numPlanets.onClick();
						numPlayers.setRight(min(10,(int)(numPlanets.getData()/2)));
						numPlayers.setData(min(numPlayers.getData(),numPlanets.getData()/2));
						data.file = scenarios.getText(scenarios.getData());
						data.noC = connectedness.getData();
						data.noPlay = numPlayers.getData();
						data.noPl = numPlanets.getData();
						data.randomGame = !isScenario.getData();
						for(int i = 0; i < 2; i++)
						{
							if(gameMode.getVal(i) == 1)
							{
								reply = (GAMETYPE)i;
								BuildUniverse(data);
								delete preview;
								preview = new universe_C();
								preview->initialize(0);
							}
						}

					}
					if(numPlayers.clicked(event.button.x, event.button.y))
					{
						numPlayers.onClick();
						data.file = scenarios.getText(scenarios.getData());
						data.noC = connectedness.getData();
						data.noPlay = numPlayers.getData();
						data.noPl = numPlanets.getData();
						data.randomGame = !isScenario.getData();
						for(int i = 0; i < 2; i++)
						{
							if(gameMode.getVal(i) == 1)
							{
								reply = (GAMETYPE)i;
								BuildUniverse(data);
								delete preview;
								preview = new universe_C();
								preview->initialize(0);
							}
						}

					
					}
				}	
				if(isScenario.clicked(event.button.x, event.button.y))
				{
					isScenario.onClick();
						data.file = scenarios.getText(scenarios.getData());
						data.noC = connectedness.getData();
						data.noPlay = numPlayers.getData();
						data.noPl = numPlanets.getData();
						data.randomGame = !isScenario.getData();
						for(int i = 0; i < 2; i++)
						{
							if(gameMode.getVal(i) == 1)
							{
								reply = (GAMETYPE)i;
								BuildUniverse(data);
								delete preview;
								preview = new universe_C();
								preview->initialize(0);
							}
					}
				}

			}
			if(event.type == SDL_MOUSEMOTION)
			{
				t->setHover(t->hover(event.motion.x,event.motion.y));
				q->setHover(q->hover(event.motion.x,event.motion.y));
			}
		}

		// Cap Frame Rate
		while(fps.get_ticks()<1000/35){}
	}
}

#endif